<?php

class DB
{
    //****START:新增数据****//
    static function insert_db($UserID, $SQL)
    {
        global $db;
        //error_log($UserID . ":1:" . $SQL . "|" . date("Y-m-d H:i:s") . "\n", 3, "/mnt/logs/sql/DB_KFYD_IN_" . date("Ymd") . ".txt");
        $intID = 0;
        $res = $db->query($SQL);
        if ($res)
        {
            $intID = $db->get_insert_id();
            if ($intID == 0)
            {
                $intID = 1;
            }
        }

        return $intID;
    }
    //****END:新增数据****//

    //****START:更新数据**** 2017.06.01//
    static function update_db($UserID, $SQL, $affected = true)
    {
        global $db;
        //error_log($UserID . ":2:" . $SQL . "|" . date("Y-m-d H:i:s") . "\n", 3, "/mnt/logs/sql/DB_KFYD_UP_" . date("Ymd") . ".txt");
        $intID = 0;
        $res = $db->query($SQL);
        if ($res)
        {
            $intID = $affected ? intval($db->get_affected_rows()) : 1;
        }
        return $intID;
    }
    //****END:更新数据****//

    //****START:删除数据****//
    static function delete_db($UserID, $SQL)
    {
        global $db;
        //error_log($UserID . ":4:" . $SQL . "|" . date("Y-m-d H:i:s") . "\n", 3, "/mnt/logs/sql/DB_KFYD_DL_" . date("Ymd") . ".txt");
        $intID = 0;
        ////babukubasic=0；babukufeeds=1
        //$strtmparr = array("okdkyao", "sysyaodian", "okdkyaologs");
        //$strDbName = $strtmparr[intval($DB)];
        //if (($strDbName) <> "")
        //{
        //    $db->selectDB($strDbName);
        $res = $db->query($SQL);
        if ($res)
        {
            $intID = 1;
        }
        //}

        return $intID;
    }
    //****END:删除数据****//

    //****START:获取数据 2017.06.01****//
    static function select_db($UserID, $SQL, $ArrType)
    {
        global $db;
        //error_log($UserID . ":6:" . $SQL . "|" . date("Y-m-d H:i:s") . "\n", 3, "/mnt/logs/sql/DB_KFYD_SL_" . date("Ymd") . ".txt");
        $time1 = time();
        $strMemo = array();
        $intsum = 0;
        $res = $db->query($SQL);
        if ($res)
        {
            if (strpos($SQL, "SQL_CALC_FOUND_ROWS"))
            {//取出总记录数
                $intsum = mysqli_fetch_row($db->query("SELECT FOUND_ROWS();"));
                $intsum = $intsum[0];
            }
            if ($ArrType == 0)
            {//一维数组值
                $rs = mysqli_fetch_assoc($res);
                if ($rs)
                {
                    $strMemo = $rs;
                    $intsum > 0 && $strMemo['max'] = $intsum;
                }
            }
            else if ($ArrType == 1)
            {//二维数组值
                $n = 0;
                while ($rd = mysqli_fetch_assoc($res))
                {
                    $strMemo[$n] = $rd;
                    $n++;
                }
                $intsum > 0 && $strMemo[0]['max'] = $intsum;
            }
        }
        $time2 = time();
        if (($time2 - $time1) > 3)
        {
            Util::logpagelogs(1, $SQL, ($time2 - $time1));
        }
        return $strMemo;
    }
    //****END:获取数据****//


    /*
        开始事物
    */
    static public function startTrans()
    {
        global $db;
        $db->startTrans();
    }

    /*
        回滚事物
    */
    static public function rollback()
    {
        global $db;
        $db->rollback();
    }

    /*
        提交事物
    */
    static public function commit()
    {
        global $db;
        $db->commit();
    }
}